<?php

App::uses('AppController', 'Controller');

/**
 * Products Controller
 *
 * @property Product $Product
 */
class ProductsController extends AppController {

  /**
   * index method
   *
   * @return void
   */
  public function index() {
    $this->Product->recursive = 0;
    $this->set('products', $this->paginate());
  }

  /**
   * view method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function view($id = null) {
    if (!$this->Product->exists($id)) {
      throw new NotFoundException(__('Invalid product'));
    }
    $options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
    $this->set('product', $this->Product->find('first', $options));
  }

  /**
   * add method
   *
   * @return void
   */
  public function add() {
    if ($this->request->is('post')) {
      $this->Product->create();
      if (($this->Product->save($this->request->data)) && ($this->updateProductShops($this->Product->getId()))) {
        $this->Session->setFlash(__('El producto ha sido creado exitosamente'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('El producto no ha sido creado'));
      }
    }
  }

  public function addStock() {

    if ($this->request->is('post')) {
      if ($this->updateStock($this->request->data['Product']['product_id'], $this->request->data['Product']['purchase_price'], $this->request->data['Product']['quantity'], $this->request->data['Product']['date'])) {
        $this->Session->setFlash(__('El stock ha sido actualizado'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('El stock NO ha sido actualizado. Please, intente nuevamente.'));
      }
    }


    $products = $this->Product->find('list');
    $this->set(compact('products'));
  }

  /**
   * edit method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function edit($id = null) {
    $this->Product->id = $id;
    if (!$this->Product->exists($id)) {
      throw new NotFoundException(__('Invalid product'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
      if ($this->Product->save($this->request->data)) {
        $this->Session->setFlash(__('El producto ha sido guardado exitosamente'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('El producto No ha sido guardado'));
      }
    } else {
      $options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
      $this->request->data = $this->Product->find('first', $options);
    }
  }

  /**
   * delete method
   *
   * @throws NotFoundException
   * @throws MethodNotAllowedException
   * @param string $id
   * @return void
   */
  public function delete($id = null) {
    $this->Product->id = $id;
    if (!$this->Product->exists()) {
      throw new NotFoundException(__('Invalid product'));
    }
    $this->request->onlyAllow('post', 'delete');
    if ($this->Product->delete()) {
      $this->Session->setFlash(__('El producto ha sido borrado exitosamente'));
      $this->redirect(array('action' => 'index'));
    }
    $this->Session->setFlash(__('El producto NO ha sido borrado'));
    $this->redirect(array('action' => 'index'));
  }

  public function updateStock($idProduct = null, $purchase_price = null, $quantity = null, $date = null) {

    $this->Product->id = $idProduct;
    $product = $this->Product->find('first', array('fields' => array(
            'Product.current_stock'), 'conditions' => array('Product.id' => $idProduct)));
    $update_stock = ($product['Product']['current_stock'] + $quantity);
    if ($this->Product->saveField('current_stock', $update_stock)) {

// Creo los datos en un array        
      $data = array(
          'HProduct' => array('shop_id' => $this->Session->read('Auth.User.shop_id'), 'product_id' => $idProduct,
              'purchase_price' => $purchase_price, 'quantity' => $quantity, 'date' => $date)
      );
      $this->loadModel('HProduct');
      $this->HProduct->create();
      if ($this->HProduct->save($data)) {
        return true;
      }
    }
  }

  public function updateProductShops($idProduct = null) {

    $shops = $this->Product->ProductShop->Shop->find('all', array('fields' => array('Shop.id')));

    foreach ($shops as $shop) {
      $this->Product->ProductShop->saveAll(array(
          'ProductShop' => array('shop_id' => $shop['Shop']['id'], 'product_id' => $idProduct)
      ));
    }
    return true;
  }

}

